#!/bin/bash

#
# Simple configure script to fit with the xrd-installer system.
#
# by Dario Berzano <dario.berzano@gmail.com>
#

#
# Globals
#

export MAKEOUT="`dirname $0`/Makefile.vars"

#
# Functions
#

# Determine which configuration method is being called
function Main() {
  if [ "$#" == 1 ] && [ "${1:0:2}" != "--" ]; then
    echo "*** Preparing configuration for xrd-installer ***"
    ConfigXrd "$1"
  elif [ "$1" == "--help" ]; then
    PrintHelp
    exit 1
  else
    echo "*** Preparing configuration for custom installation ***"
    ConfigCustom "$@"
  fi

  echo ""
  PrintConfig
  WriteConfig
  exit 0
}

# Detects the underlying operating system - for now it detects Ubuntu/Debian
# and defaults to RedHat-based in any other case
# Inspired from http://www.novell.com/coolsolutions/feature/11251.html
function DetectOs() {
  if [ -e "/etc/lsb-release" ] || [ -e "/etc/debian-release" ]; then
    export OS="Debian"
  else
    export OS="RedHat"
  fi
  #echo "Detected OS is $OS"
}

# Prints the help
function PrintHelp() {
  echo "Parameters:"
  echo "  --prefix=<prefix>  the installation prefix (default=/)"
  echo "  --with-rootsys     the directory of ROOT (default=<autodetect>)"
}

# Prints the configuration
function PrintConfig() {
  echo "Installation variables:"
  echo " * ROOTSYS       = $ROOTSYS"
  echo " * BINPREFIX     = $BINPREFIX"
  echo " * ETCPREFIX     = $ETCPREFIX"
  echo " * XRD_INSTALLER = $XRD_INSTALLER" >> "$MAKEOUT"
}

# Saves the configuration to the output file
function WriteConfig() {
  echo "# Auto-generated by configure script on `date +'%Y-%m-%d %H:%M:%S'`" \
    > "$MAKEOUT"
  echo "export ROOTSYS = $ROOTSYS" >> "$MAKEOUT"
  echo "export BINPREFIX = $BINPREFIX" >> "$MAKEOUT"
  echo "export ETCPREFIX = $ETCPREFIX" >> "$MAKEOUT"
  echo "export XRD_INSTALLER = $XRD_INSTALLER" >> "$MAKEOUT"
}

# Configures variables if called from xrd-installer
function ConfigXrd() {
  local PREFIX="$1"
  export ROOTSYS=`cd "$PREFIX" 2>/dev/null && cd ../root 2>/dev/null && pwd`
  export BINPREFIX=`PurgeSlashes "$PREFIX/bin"`
  export ETCPREFIX=`PurgeSlashes "$PREFIX/etc"`
  export XRD_INSTALLER=1
  if [ ! -d "$ROOTSYS" ]; then
    echo "ROOT not found, aborting!"
    exit 3
  fi
}

# Eliminates double slashes and trailing slash too
function PurgeSlashes() {
  which perl > /dev/null 2>&1
  if [ $? != 0 ]; then
    echo "$1"
    return
  fi
  local STR="$1"
  local NEWSTR=""
  while [ 1 ]; do
    NEWSTR=`echo "$STR" | perl -ne 's/\/\//\//g; print $_'`
    if [ "$NEWSTR" == "$STR" ]; then
      break
    fi
    STR="$NEWSTR"
  done
  echo $STR | perl -ne 's/(.*)\/$/$1/; print $_'
}

# Configures variables in a custom way
function ConfigCustom() {
  local PREFIX="/"
  export ROOTSYS=""

  while [ $# -gt 0 ]; do
    case $1 in

      --prefix=*)
        PREFIX=${1:9}
      ;;

      --with-rootsys=*)
        ROOTSYS=${1:15}
      ;;

      *)
        echo "Unrecognized option: $1" >&2
        exit 2
      ;;

    esac
    shift
  done

  # Check for ROOTSYS
  echo -n "Searching for ROOT installation..."
  if [ "$ROOTSYS" == "" ]; then

    # Try to find ROOT automatically on the whole system
    ROOTEXE="root.exe"

    which locate > /dev/null 2>&1
    if [ $? == 0 ]; then
      ROOTSYS=`locate --limit 1 "$ROOTEXE" 2> /dev/null | head -n1` 
    fi

    # If locate failed, let's try with find (veeery slow)
    #if [ "$ROOTSYS" == "" ]; then
    #  ROOTSYS=`find / -name "$ROOTEXE" -print -quit 2> /dev/null`
    #fi

    if [ "$ROOTSYS" != "" ]; then
      ROOTSYS=`dirname "$ROOTSYS"`
      ROOTSYS=`dirname "$ROOTSYS"`
      ROOTSYS=`cd "$ROOTSYS" ; pwd`
    fi

  fi

  if [ -e "$ROOTSYS/bin/$ROOTEXE" ]; then
    echo "found in $ROOTSYS"
  else
    echo "failed, specify a path with --with-rootsys=<dir_of_root>"
    exit 3
  fi

  export BINPREFIX=`PurgeSlashes "$PREFIX/usr/bin"`
  export ETCPREFIX=`PurgeSlashes "$PREFIX/etc"`

  export XRD_INSTALLER=0
}

#
# Entry point
#

Main "$@"
